import 'package:example/router/example_routes.dart';
import 'package:example/util/widget_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:riki_router/riki_router.dart';

@RikiRoute(
  name: '/hero/demo1',
  routeName: '测试hero动画',
  pageRouteType: PageRouteType.cupertino,
  transitionMilliseconds: 4000,
  reverseTransitionMilliseconds: 2000,
)
class Hero1Page extends StatefulWidget {
  const Hero1Page() : super();

  @override
  _Hero1PageState createState() => _Hero1PageState();
}

class _Hero1PageState extends State<Hero1Page> {
  @override
  Widget build(BuildContext context) {
    List<String> keys = RikiRouterDelegate.of(context).findHeroKeys(widget);
    if(keys.isEmpty){
      return _buildBody(['无Hero']);
    }
    return Hero(
      tag: keys.first,
      flightShuttleBuilder: heroFlightShuttleBuilder,
      child: _buildBody(keys),
    );
  }

  Scaffold _buildBody(List<String> keys) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hero动画'),
        backgroundColor: Colors.transparent,
      ),
      extendBodyBehindAppBar: true,
      body: Column(
        children: [
          Image.network(
            'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F031H0111515%2F20031G11515-9-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647162193&t=ddf7ddb44ba310f4b108cebb73de9549',
            fit: BoxFit.cover,
            width: double.infinity,
            height: 440,
          ),
          SizedBox(height: 16),
          Row(
            children: [
              SizedBox(width: 16),
              Text(
                '${keys.first}测试',
              ),
              SizedBox(width: 60),
              Expanded(
                child: Text(
                  '${keys.first}测试-----',
                ),
              ),
              SizedBox(width: 16),
            ],
          ),
          SizedBox(height: 16),
        ],
      ),
    );
  }
}

Widget heroFlightShuttleBuilder(
  BuildContext flightContext,
  Animation<double> animation,
  HeroFlightDirection flightDirection,
  BuildContext fromHeroContext,
  BuildContext toHeroContext,
) {
  final Hero toHero = toHeroContext.widget as Hero;
  if(fromHeroContext.widget is! Hero){
    return toHero.child;
  }
  RenderBox box = fromHeroContext.findRenderObject() as RenderBox;
  RenderBox box1 = toHeroContext.findRenderObject() as RenderBox;
  Offset position = box.localToGlobal(Offset.zero);
  Offset position1 = box1.localToGlobal(Offset.zero);
  final Hero fromHero = fromHeroContext.widget as Hero;
  return AnimatedPositioned(
    duration: Duration(milliseconds: 400),
    top: Tween<double>(begin: position.dy, end: position1.dy).animate(animation).value,
    width: Tween<double>(begin: box.size.width, end: box1.size.width).animate(animation).value,
    height: Tween<double>(begin: box.size.height, end: box1.size.height).animate(animation).value,
    left: Tween<double>(begin: position.dx, end: position1.dx).animate(animation).value,
    child: TweenAnimationBuilder<double>(
      duration: Duration(milliseconds: 200),
      builder: (_, value, child) {
        return value > 0.5 ? toHero.child : fromHero.child;
      },
      tween: Tween<double>(begin: 0, end: 1),
    ),
  );
}
